<?php
	require_once '../conn/DBconnect.php';
	require_once '../email/registration.php';
	
	session_start();
	$emailErr = "Invalid email";
	
	if(isset($_SESSION['paypalToken']) && isset($_SESSION['paypalPayerID']))
	{
		unset($_SESSION['paypalToken']);
		unset($_SESSION['paypalPayerID']);
	}
	
	unset($_SESSION['r_race_code']);
	unset($_SESSION['r_firstName']);
	unset($_SESSION['r_lastName']);
	unset($_SESSION['r_middle_ini']);
	unset($_SESSION['r_gender']);
	unset($_SESSION['r_birthdate']);
	unset($_SESSION['r_email']);
	unset($_SESSION['r_mobile']);
	unset($_SESSION['r_nationality']);
	unset($_SESSION['r_country']);
	unset($_SESSION['r_address1']);
	unset($_SESSION['r_address2']);
	unset($_SESSION['r_delivery_add']);
	unset($_SESSION['r_contact_name']);
	unset($_SESSION['r_contact_phone']);
	
	unset($_SESSION['r_size']);
	unset($_SESSION['r_color']);
	
	//session_destroy();
	
	$params[0] = "INSERT";
	$params[1] = "0";
 	$params[2] = $_POST["r_code"];
 	$params[3] = $_POST["r_firstName"];
 	$params[4] = $_POST["r_lastName"];
 	$params[5] = $_POST["r_middle_ini"];
 	$params[6] = $_POST["r_gender"];
 	$params[7] = $_POST["r_birthdate"];
 	$params[8] = $_POST["r_email"];
 	$params[9] = $_POST["r_mobile"];
 	$params[10] = $_POST["r_nationality"];
 	$params[11] = $_POST["r_country"];
 	$params[12] = $_POST["r_address1"];
 	$params[13] = $_POST["r_address2"];
 	$params[14] = $_POST["r_delivery_add"];
 	$params[15] = $_POST["r_contact_name"];
 	$params[16] = $_POST["r_contact_phone"];
 	$params[17] = $_POST["r_size"];
 	$params[18] = $_POST["r_color"];
 	$params[19] = $_POST["r_reg_outlet"];
 	$params[20] = $_POST["r_reg_cat"];
 	$params[21] = $_POST["r_promo_no"];
 	$params[22] = $_POST["r_race_code"];
 	$params[23] = $_POST["r_bib"];
 	$params[24] = $_SESSION["transID"];
 	
 	//if unregistered user to the event
 	if(isset($_SESSION['loggedUser']) && $_SESSION['isRegistered'] == false)
	{
		$params[0] = "UPDATE_RECORD";
		$params[1] = $_SESSION["loggedUser"]["pi_id"];
	}
 	
 	if (filter_var($_POST["r_email"], FILTER_VALIDATE_EMAIL)) {
    	$insertPROC = "CALL SP_REGISTRATION('" . implode("', '", $params) . "');";
	 	$result = mysqli_query($mysqlLink, $insertPROC);
	 	
	 	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	 	echo "<h2>".$row["err_code"]."</h2>";
	 	
	 	
	 	if( $row["err_id"] != '-1')
	 	{
	 	?>
	 		<div id="registrationPrint" style="width:400px;text-align: center;">
				<div><img src="../images/cmr_logo.png"/></div>
				<div class="divSpacer"></div>
				<div class="divSpacer"></div>
				<div><img src="../images/proactive_logo.png"/></div>
				
				<div style="text-align: center;">
					<h2>Thanks for coming</h2>
					<h2>-and-</h2>
					<h2>Thank you for signing up!</h2>
					<br>
					<div style="padding-left: 30px;">
						<div style="text-align: left;">Details of your registration are as follows:</div>
						<div style="text-align: left;"><b>Registration No.: <?php echo $row["pi_code"]?></b></div>
						<div style="text-align: left;"><b>Registration Date: <?php echo $row["pi_crd"]?></b></div>
						<div style="text-align: left;"><b>Participant	: <?php echo $_POST["r_firstName"]." ".$_POST["r_lastName"]." ".$_POST["r_middle_ini"]?></b></div>
						<div style="text-align: left;"><b>BirthDate		: <?php echo $_POST["r_birthdate"] ?></b></div>
						<div style="text-align: left;"><b>Gender		: <?php echo $_POST["r_gender"] ?></b></div>
						<div style="text-align: left;"><b>Race Category	: <?php echo $_POST["r_race_code"] ?></b></div>
						<div style="text-align: left;"><b>Singlet		: <?php echo $_POST["r_size"] ?></b></div>
						<div style="text-align: left;"><b>Color		: <?php echo $_POST["r_color"] ?></b></div>
						<br>
						<div style="text-align: left;"><b>Fee		: Php <?php echo $row["totalRegFee"]?></b></div>
						
						<?php echo RegEmail::emailNotificationText(); ?>
					</div>
				</div>
			</div>
			<input type="hidden" name="reg_code" value="<?php echo $row["pi_code"]?>"/>
			<input type="hidden" name="reg_date" value="<?php echo $row["pi_crd"]?>"/>
			<input type="hidden" name="reg_fee" value="<?php echo $row["rc_reg_fee"]?>"/>
			
		<?php 
			
			if(isset($_SESSION['isRegistered']) && $_SESSION['isRegistered'] == false)
				$_SESSION['isRegistered'] = true;
		//echo $row["defaultPassword"];
		
			$message = '
				<html>
				<head>
				  <title>Color Manila Run</title>
				</head>
				<body>
				  <div><img src="http://www.proactive.ph/colormanilarun/images/cmr_logo.jpg"/></div>
				  <div class="divSpacer"></div>
					<div><img src="http://www.proactive.ph/colormanilarun/images/proactive_logo.png"/></div>
					
					<h2>Thank you for signing up!</h2>
					<br>
					<div style="padding-left: 30px;">
						<div style="text-align: left;">Details of your registration are as follows:</div>
						<div style="text-align: left;"><b>Registration No.: ' . $row["pi_code"]. '</b></div>
						<div style="text-align: left;"><b>Registration Date: ' . $row["pi_crd"] . '</b></div>
						<div style="text-align: left;"><b>Participant	: ' . $_POST["r_firstName"].' '.$_POST["r_lastName"].' ' .$_POST["r_middle_ini"] .'</b></div>
						<div style="text-align: left;"><b>BirthDate		: ' . $_POST["r_birthdate"] . '</b></div>
						<div style="text-align: left;"><b>Gender		: ' . $_POST["r_gender"] . '</b></div>
						<div style="text-align: left;"><b>Race Category	: ' . $_POST["r_race_code"] . '</b></div>
						<div style="text-align: left;"><b>Singlet		: ' . $_POST["r_size"] . '</b></div>
						<div style="text-align: left;"><b>Color		: ' . $_POST["r_color"] . '</b></div>
						<br>
						<div style="text-align: left;"><b>Fee		: Php ' . $row["totalRegFee"]. '</b></div>
						<br>
						<span>You can now login at <a href="www.proactive.ph/colormanilarun">www.proactive.ph/colormanilarun</a> 
						using the temporary password: <b>'. $row["defaultPassword"].'</b> </span>
						'. RegEmail::emailNotificationText().'						
				</body>
				</html>
				';
			
			RegEmail::registrationComplete($_POST["r_email"], $message);
	 	}

	 	echo "<input type=\"hidden\" name=\"err_id\" value=\"".$row["err_id"]."\"/>";
	 	
	 	mysqli_free_result($result);
	 	
	 	mysqli_close($mysqlLink);
	 	
	}
	else
		echo "<h2>".$emailErr."</h2>";
		
?>